package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import javaben.Student;
import utildb.DButil;

import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.JScrollPane;
import javax.swing.JLabel;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;
import java.awt.event.ActionEvent;

public class MyDBJFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTable table;
	DButil dbu=new DButil();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MyDBJFrame frame = new MyDBJFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MyDBJFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 622, 477);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnDb = new JMenu("DB");
		menuBar.add(mnDb);
		
		JMenuItem mntmConnect = new JMenuItem("Connect");
		
		mnDb.add(mntmConnect);
		
		JMenuItem mntmDisconnect = new JMenuItem("DisConnect");
		mntmDisconnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				dbu.closeAll();
			}
		});
		mnDb.add(mntmDisconnect);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		textField = new JTextField();
		textField.setBounds(121, 10, 323, 33);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JButton btnSearch = new JButton("Search");
		
		btnSearch.setBounds(466, 9, 93, 34);
		contentPane.add(btnSearch);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(30, 68, 529, 318);
		contentPane.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		JLabel lblInput = new JLabel("Input:");
		lblInput.setBounds(57, 10, 54, 33);
		contentPane.add(lblInput);
		
		mntmConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//get DB connection
				Connection con=dbu.getDBConnection("mysql");
				if(con!=null) {
					JOptionPane.showMessageDialog(null, "DB Connected!");
				}
			}
		});
		
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String input=textField.getText().trim();
				
				
				Vector<String> v=new Vector<String>();
				v.add("ID");
				v.add("Name");
				v.add("Sage");
				v.add("Score");
				v.add("Sdept");
				DefaultTableModel df=new DefaultTableModel(v,0);
				
//				if(input.length()!=0) {
					ArrayList<Student> arlist=dbu.queryById(input);
					for(Student s:arlist) {
						String[] line= {s.getSid(),s.getSname(),String.valueOf(s.getSage()),String.valueOf(s.getScore()),s.getSdept()};
						df.addRow(line);
						System.out.println(line);
					}
					table.setModel(df);
			}
		});
	}
}
